/**
 * @name 数据表stu_h1802操作对象
 * @author 子瓜
 * @version 2.1
 * @requires BaseService
 * @class Stu_h1802Service
 * @description 2.0版本 增同Promise，实现继承
 */




const Stu_h1802=require("../model/Stu_h1802");
const BaseService=require("./BaseService");
const ObjectHelper=require("../utils/ObjectHelper");
const PageList = require("../model/PageList");

class Stu_h1802Service extends BaseService{
    constructor(){
        super("stu_h1802");    //实现继承以后，一定要调用super
    }
    /**
     * @name 根据学号与QQ号进行登陆检测
     * @param {Object} paramSql  学号与QQ号
     * @returns {Promise} 异步操作
     */
    checkLogin({s_id,s_qq}){
        return new Promise((resolve,reject)=>{
           let conn=super.getConn();
           let strSql=`select * from ${this.tableName} where s_id=? and s_qq=?`;
           conn.query(strSql,[s_id,s_qq],(err,result)=>{
               if(err){
                   reject(err);
               }
               else{
                   resolve(result);
               }
           });
           conn.end();
        })
        
    }

    

    /**
      * @name 获取所有信息
      * @returns {Promise} 异步操作
      */
     getAllList(){
         return new Promise((resolve,reject)=>{
             let conn = super.getConn();
             conn.query(`select * from ${this.tableName}`,[],(err,result)=>{
                 if(err){
                     reject(err);
                 }else{
                     resolve(result);
                 }
             })
             conn.end();
         });
     }


     /**
      * @name 新增学生信息
      * @param {Stu_H1802} model 
      * @returns {Promise} 返回操作结果
      */
     addStudent(model){
        return new Promise((resolve,reject)=>{
            let values=Object.values(model);
            let conn =super.getConn();
            let insertSql=super.createInsertSql(model);
            conn.query(insertSql,values,(err,result)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(result);
                }
                conn.end();
            })
        }) 
     }

     queryByList({s_id,s_name,s_sex}, pageIndex){
         return new Promise((resolve,reject)=>{
             let conn=super.getConn();
             let strSql = `select * from ${this.tableName} where 1=1 `;
             let strCountSql=`select count(*) 'sumCount' from ${this.tableName} where 1=1 `;
             let paramSql = "";
             if(!ObjectHelper.isNullorUndefined(s_id)){
                paramSql+=`and s_id like "%${s_id}%"`;
             }
             if(!ObjectHelper.isNullorUndefined(s_name)){
                paramSql+=`and s_name like "%${s_name}%"`;
            }
            if(!ObjectHelper.isNullorUndefined(s_sex)){
                paramSql+=`and s_sex = "${s_sex}"`;
            }
            strSql+= paramSql + "limit ?,?";
            strCountSql += paramSql;
            conn.query(strSql + ";" + strCountSql,[(pageIndex - 1)*5,5],(err,result)=>{
                if(err){
                    reject(err);
                }else{
                    let sumCount=result[1][0].sumCount;
                    let pageList= new PageList(pageIndex,sumCount,result[0]);
                    resolve(pageList);
                }
            })
            conn.end();
         })
     }

    /**
     * @name 根据学号删除记录
     * @param {String} s_id 要根据删除的学号 
     * @returns {Promise} 返回数据库操作结果Promise
     */
     deleteById(s_id){
          return new Promise((resolve,reject)=>{
              let conn=super.getConn();
              let deleteSql=`delete from ${this.tableName} where s_id=? `;
              conn.query(deleteSql,[s_id],(err,result)=>{
                  if(err){
                    reject(err);
                  }else{
                    //   let flag=false;//用来表示成功或失败
                    // if(result.affectedRows>0){
                    //     flag=true;//成功
                    // }else{
                    //     flag=false;//失败
                    // }

                    let flag=result.affectedRows>0?true:false;

                    resolve(flag);
                  }
              });
              conn.end();
          })
     }




     /**
     * @name 批量删除
     * @param {Array} s_idArr 要批量删除的学号的数组 
     * @returns {Promise}  返回执行数据库以后的操作
     */
    deleteByChecked(s_idArr){
        return new Promise((resolve,reject)=>{
            let conn = super.getConn();
            let deleteSql=`delete from ${this.tableName} where s_id in (${new Array(s_idArr.length).fill("?").toString()})`;
            conn.query(deleteSql,s_idArr,(err,result)=>{
                if(err){
                    reject(err);
                }else{
                    let flag=result.affectedRows>0?true:false;
                    resolve(flag);
                }
            });
            conn.end();
        })
    }

/**
 * @name 根据学号得到学生信息
 * @param {String} s_id 
 * @returns {Promise} 返回执行数据库以后的操作
   */
    findByS_id(s_id){
        return new Promise((resolve,reject)=>{
            let conn=super.getConn();
            let strSql=`select * from ${this.tableName} where s_id=? `;
            conn.query(strSql,[s_id],(err,result)=>{
                if(err){
                    reject(err);
                }else{
                    resolve(result);
                }
            })
            conn.end();
        })
    }

    /**
     * @name 修改学生信息
     * @param {Object} model 要修改的对象 
     * @returns {Promise}  返回执行数据库以后的操作
     */
    editStudent(model){
        return new Promise((resolve,reject)=>{
            let {s_name,s_sex,s_age,s_qq,s_school,s_major,s_education,s_id}=model
            let updateSql=`update ${this.tableName} set s_name=?,s_sex=?,s_age=?,s_qq=?,s_school=?,s_major=?,s_education=? where s_id=?`;
            let conn=super.getConn();
            conn.query(updateSql,[s_name,s_sex,s_age,s_qq,s_school,s_major,s_education,s_id],(err,result)=>{
                if(err){
                    reject(err);
                }else{
                    let flag=result.affectedRows>0?true:false;
                    resolve(flag);
                }
            })
            conn.end();
        })
    }



 }



 module.exports=Stu_h1802Service;
 